﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;

namespace GetSmartIDEList.Lib.ORM
{
    /// <summary>
    /// 用来拼接操作数据库的字符串
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class InstallDBTable<T>
    {
        /// <summary>
        /// 拼接查询字符串(只能用在与表建立联系的实体类)
        /// </summary>
        /// <param name="types"> 与表建立映射的自定义类</param>
        /// <returns>查询字符串</returns>
        public string InsertDate(T types)
        {
            string cmdtxt = "insert into ";
            string cmdparVar = null;
            Type userAttu = types.GetType();
            TableAttribute tables = (TableAttribute)userAttu.GetCustomAttributes(false)[2];
            cmdtxt += tables.TableName + "(";
            PropertyInfo[] info = userAttu.GetProperties();

            foreach (PropertyInfo prs in info)
            {
                object[] attu = prs.GetCustomAttributes(false);

                foreach (Attribute abute in attu)
                {
                    if (abute is FieldAttribute)
                    {
                        FieldAttribute midle = abute as FieldAttribute;
                        cmdtxt += midle.Fields + ",";
                        object obj = prs.GetGetMethod().Invoke(types, null);
                        if (midle.Dbtype == DbType.Int32)
                            cmdparVar += obj + ",";
                        else if (obj is null)
                        {
                            cmdparVar += "null,";
                        }
                        else cmdparVar += "'" + obj + "',";
                    }
                }
            }

            cmdparVar = cmdparVar.Substring(0, cmdparVar.Length - 1);
            cmdtxt = cmdtxt.Substring(0, cmdtxt.Length - 1) + ")";

            cmdtxt += " values(" + cmdparVar + ")";
            return cmdtxt;
        }

        /// <summary>
        /// 获取Update的语句
        /// </summary>
        /// <param name="types"></param>
        /// <param name="typeWhere">where条件</param>
        /// <returns></returns>
        public string UpdateDate(T types, T typeWhere)
        {
            string cmdtxt = "update " + SetSQLFields(types);
            string cmdWhere = null;

            if (typeWhere != null)
            {
                cmdWhere = SetSQLFields(typeWhere, true);
            }
            cmdtxt += " where 1=1 " + cmdWhere;
            return cmdtxt;
        }

        /// <summary>
        /// 删除Delete的语句
        /// </summary>
        /// <param name="types"></param>
        /// <returns></returns>
        public string DeleteData(T types)
        {
            string cmdtxt = "delete from ";
            Type userAttu = types.GetType();

            TableAttribute tables = (TableAttribute)userAttu.GetCustomAttributes(false)[2];
            cmdtxt += tables.TableName + " where ";

            PropertyInfo[] info = userAttu.GetProperties();

            foreach (PropertyInfo prs in info)
            {
                object[] attu = prs.GetCustomAttributes(false);
                foreach (Attribute abute in attu)
                {
                    if (abute is FieldAttribute)
                    {
                        FieldAttribute midle = abute as FieldAttribute;
                        if (midle.PK_Primary)
                        {
                            cmdtxt += midle.Fields + "=";
                            object obj = prs.GetGetMethod().Invoke(types, null);
                            if (midle.Dbtype == DbType.Int32)
                                cmdtxt += obj;
                            else
                                cmdtxt += "'" + obj + "'";
                            break;
                        }
                    }
                }
            }
            return cmdtxt;
        }

        /// <summary>
        /// 设置SQL字段
        /// </summary>
        /// <param name="types"></param>
        /// <param name="isWhere"></param>
        /// <returns></returns>
        private static string SetSQLFields(T types, bool isWhere = false)
        {
            Type userAttu = types.GetType();
            string cmdtxt = null;

            if (!isWhere)
            {
                TableAttribute tables = (TableAttribute)userAttu.GetCustomAttributes(false)[2];
                cmdtxt += tables.TableName + " set ";
            }
            PropertyInfo[] info = userAttu.GetProperties();

            foreach (PropertyInfo prs in info)
            {
                object[] attu = prs.GetCustomAttributes(false);

                foreach (Attribute abute in attu)
                {
                    if (abute is FieldAttribute)
                    {
                        FieldAttribute midle = abute as FieldAttribute;

                        if (!midle.IsIgnore)
                        {
                            cmdtxt += (isWhere ? "AND " : "") + midle.Fields + "=";
                            object obj = prs.GetGetMethod().Invoke(types, null);
                            if (midle.Dbtype == DbType.Int32)
                                cmdtxt += obj + (isWhere ? " " : ",");
                            else
                                cmdtxt += "'" + obj + "'" + (isWhere ? " " : ",");
                        }
                    }
                }
            }

            cmdtxt = cmdtxt.Substring(0, cmdtxt.Length - 1);
            return cmdtxt;
        }
    }
}